import {
  getSendDataTable
} from '../../api/sms'
import {
  SEND_DATA_LIST,
  SEND_DATA_DATA_COUNT,
  SEND_DATA_HISTORY_DATA,
  SEND_DATA_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    sendDataList: [],
    sendDataDataCount: null,
    sendDataPageSize: 20,
    sendDataHistoryData: [],
    row: {
      ctime: '',
      name: '',
      cnt: '',
      groupname: '',
      state: ''
    }
  },
  getters: {
    sendDataList: state => state.sendDataList
  },
  mutations: {
    [SEND_DATA_LIST] (state, data) {
      state.sendDataList = data
    },
    [SEND_DATA_DATA_COUNT] (state, data) {
      state.sendDataDataCount = data
    },
    [SEND_DATA_HISTORY_DATA] (state, data) {
      state.sendDataHistoryData = data
    },
    [SEND_DATA_PAGE_SIZE] (state, data) {
      state.sendDataPageSize = data
    }
  },
  actions: {
    async getSendDataList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getSendDataTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          name: item.name,
          cnt: item.cnt,
          groupname: item.groupname,
          state: item.state
        }]
      })
      commit(SEND_DATA_LIST, newData)
      commit(SEND_DATA_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.sendData.sendDataPageSize) {
        commit(SEND_DATA_HISTORY_DATA, newData)
      } else {
        commit(SEND_DATA_HISTORY_DATA, newData.slice(0, rootState.sendData.sendDataPageSize))
      }
      return resp
    },
    async changeSendDataPage ({ commit, rootState }, index) {
      let data = rootState.sendData.sendDataList
      let _start = (index - 1) * rootState.sendData.sendDataPageSize
      let _end = index * rootState.sendData.sendDataPageSize
      commit(SEND_DATA_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeSendDataPageSize ({ commit, rootState }, value) {
      commit(SEND_DATA_PAGE_SIZE, value)
    }
  }
}
